package com.ald.crossex.converter.wechat;

import org.apache.log4j.Logger;
import org.springframework.core.convert.converter.Converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class StringToDateConverter implements Converter<String, Date> {


    Logger log = Logger.getLogger(StringToDateConverter.class);
    /**
     * 转化格式
     */
    private String convertParent;

    private SimpleDateFormat simpleDateFormat;

    public String getConvertParent() {
        return convertParent;
    }

    public void setConvertParent(String convertParent) {
        this.convertParent = convertParent;
    }

    @Override
    public Date convert(String source) {
        try {
            if (simpleDateFormat == null) {
                simpleDateFormat = new SimpleDateFormat(convertParent);
            }

            return simpleDateFormat.parse(source);
        } catch (ParseException e) {
            log.error("convert string to date failed! " + e.getMessage());
        }
        return null;

    }
}